package com.ymatou.iapi.settlement.testcase;

import java.util.List;
import java.util.stream.Collectors;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Ignore;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.ymatou.iapi.optrade.parameter.enums.LogisticsTypeEnum;
import com.ymatou.iapi.optrade.resp.TradingServiceResp;
import com.ymatou.iapi.settlement.parameter.ReceiveRtnCommissionResultBean;
import com.ymatou.iapi.settlement.service.ReceiveRtnCommissionResultCall;
import com.ymatou.iapi.synctrade.service.TradingSyncService;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.TestCase;
import com.ymt.utils.annotation.TestSuite;
import com.ymt.utils.tag.P1;
import com.ymttest.business.service.YMTTrading.TradingCallService;
import com.ymttest.business.service.YMTTrading.TradingPlaceOrderCallService;
import com.ymttest.database.model.YmtSettlementInvoice;
import com.ymttest.database.sqlwapper.YmtSettlementInvoiceWapper;

/**
 * 接收返佣结果接口 FreeMaker Template自动生成代码
 */
@TestSuite
@Ignore
public class Ts_ReceiveRtnCommissionResult {

	private static ReceiveRtnCommissionResultBean receivertncommissionresultBean;
	private static ReceiveRtnCommissionResultCall receivertncommissionresultCall;

	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("接收返佣结果接口");
	}

	@Before
	public void caseUp() {
		receivertncommissionresultBean = new ReceiveRtnCommissionResultBean();
		receivertncommissionresultCall = new ReceiveRtnCommissionResultCall();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(receivertncommissionresultCall.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}

	/**
	 * 一个订单下面多个商品
	 
	private Map placeOrder() {
		Map map = new HashMap();
		try {
			PlaceOrderReq placeorderBean = new PlaceOrderReq();
			PlaceOrderCall placeorderCall = new PlaceOrderCall();
			int userId = Integer.valueOf(EnvSetup.getData("BuyUserId"));
			int sellerId = Integer.valueOf(EnvSetup.getData("SellerId"));
			// 创建产品
			Logger.comment("前置数据准备");
			HashMap<String, Object> productInfo = ProductCallService
					.createProduct(sellerId, 3);
			Logger.comment("前置数据准备结束");
			String productId = (String) productInfo.get("productId");
			Logger.debug("productId:" + productId);
			String productName = (String) productInfo.get("productName");
			String pic = (String) productInfo.get("pic");
			BigDecimal price = (BigDecimal) (productInfo.get("price"));
			String catalogId = (String) productInfo.get("catalogId");

			placeorderBean.setAppId("AutoTest");
			placeorderBean.setUserId(userId);
			placeorderBean.setRequestId(TestDataManager.getRandomUUID()
					.toString());
			placeorderBean.setTerminalSource(TerminalSourceEnum.IOS);
			placeorderBean.setAppTerminalSource(AppTerminalSourceEnum.Phone);
			placeorderBean.setSourceIp("127.0.0.1");
			placeorderBean.setAppId("ios");
			placeorderBean.setDeviceId("7777777777888888888888999");
			OrderGroupVo groupVo1 = new OrderGroupVo();
			placeorderBean.setOrderSource(OrderSourceEnum.PC);

			groupVo1.setSellerId(sellerId);
			List<OrderVo> orders = new ArrayList<OrderVo>();
			OrderVo ordervo1 = new OrderVo();
			ordervo1.setAutoCancelOrderHours(new BigDecimal(1));
			ordervo1.setOrderType(OrderTypeEnum.Spot);
			ordervo1.setPaidFull(true);
			ordervo1.setAddress("上海市闸北区灵石路636号");
			ordervo1.setLeaveWord("ymt autotest");
			ordervo1.setPhone("13100000001");
			ordervo1.setPostCode("200001");
			ordervo1.setTelephone("021-51002100");
			ordervo1.setReceiveName("李四");
			List<OrderItemVo> items = new ArrayList<OrderItemVo>();
			OrderItemVo orderitem1 = new OrderItemVo();
			orderitem1.setCatalogId(TestDataManager.getRandomUUID().toString());
			orderitem1.setProductNum(3);
			orderitem1.setProductName(productName);
			orderitem1.setPriceType(PriceTypeEnum.VipPrice);
			orderitem1.setFirstCategoryId(SettlementService.firstCataid);
			orderitem1.setSecondCategoryId(SettlementService.secondCategoryId);
			orderitem1.setProductId(TestDataManager.getRandomUUID().toString());
			price = new BigDecimal(210);
			orderitem1.setEarnestPrice(price);
			orderitem1.setProductPrice(price);
			orderitem1.setOriginalPrice(price);
			orderitem1.setSalesType(SalesTypeEnum.AlreadyInStock);
			orderitem1.setLogisticsType(LogisticsTypeEnum.SailProtect);
			orderitem1
					.setProductRefundChannel(ProductRefundChannelEnum.Official);
			orderitem1.setBrandId(5);
			orderitem1.setPackageNo("11111111");
			orderitem1
					.setProductDetailInfo("e5a1eccf-6a14-458f-b907-35dc9a3d2713#适用人群:b13e9eb1-98d6-4d39-8c35-225758e443d3#男");
			orderitem1.setProductRecordNo("1234567");
			orderitem1
					.setProductRefundChannel(ProductRefundChannelEnum.Official);
			orderitem1
					.setProperty("06f25e0c-e431-44ae-8a07-4d0bc824e3bd#尺寸:951a814e-2ded-4241-b9a0-91b2cd9a44fd#6,d2f663ea-3214-4344-abce-a8eaffcc43ce#颜色:f3ed3ad7-a084-450b-b698-256542963306#红色");

			ordervo1.setOrderItems(items);
			ordervo1.setFreight(new BigDecimal("21"));
			// 第2件商品
			OrderItemVo orderitem2 = new OrderItemVo();
			orderitem2.setCatalogId("edbcc2c9-1ba9-4b04-ab88-940f63271823");
			orderitem2.setProductNum(1);
			orderitem2.setProductName(productName);
			orderitem2.setPriceType(PriceTypeEnum.VipPrice);
			orderitem2.setProductId(TestDataManager.getRandomUUID().toString());
			orderitem2.setFirstCategoryId(SettlementService.firstCataid);
			orderitem2.setSecondCategoryId(SettlementService.secondCategoryId);

			price = new BigDecimal(70);
			orderitem2.setEarnestPrice(price);
			orderitem2.setProductPrice(price);
			orderitem2.setOriginalPrice(price);
			orderitem2.setSalesType(SalesTypeEnum.AlreadyInStock);
			orderitem2.setLogisticsType(LogisticsTypeEnum.SailProtect);
			orderitem2
					.setProductRefundChannel(ProductRefundChannelEnum.Official);
			orderitem2.setBrandId(5);
			orderitem2.setPackageNo("11111111");
			orderitem2
					.setProductDetailInfo("e5a1eccf-6a14-458f-b907-35dc9a3d2713#适用人群:b13e9eb1-98d6-4d39-8c35-225758e443d3#男");
			orderitem2.setProductRecordNo("1234567");
			orderitem2
					.setProductRefundChannel(ProductRefundChannelEnum.Official);
			orderitem2
					.setProperty("06f25e0c-e431-44ae-8a07-4d0bc824e3bd#尺寸:951a814e-2ded-4241-b9a0-91b2cd9a44fd#6,d2f663ea-3214-4344-abce-a8eaffcc43ce#颜色:f3ed3ad7-a084-450b-b698-256542963306#红色");
			items.add(orderitem1);
			items.add(orderitem2);
			ordervo1.setOrderItems(items);
			ordervo1.setFreight(new BigDecimal("21"));
			orders.add(ordervo1);
			groupVo1.setOrders(orders);
			placeorderBean.setOrderGroups(Arrays.asList(groupVo1));
			placeorderCall.setData(false, placeorderBean);
			placeorderCall.callService();
			JSONArray orderIds = placeorderCall.getOrderIds();
			int tradingId = placeorderCall.getTradingId();
			map.put("orderIds", orderIds);
			map.put("tradingId", tradingId);

		} catch (Exception e) {
			Logger.fail(e);
		}
		return map;
	}*/

	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_ReceiveRtnCommissionResult_001() {
		Logger.start(true, "接收返佣结果类型为无需返佣");
		try {

			Logger.comment("前置数据准备");
			// 创建一个拼邮的订单
			TradingServiceResp placeOrderResp = TradingPlaceOrderCallService
					.placeOrderWithLogisticsType(LogisticsTypeEnum.OVERSEAS_DELIVERY);

			long orderId = placeOrderResp.getOrderList().get(0).getOrderId();
			long sellerId = placeOrderResp.getOrderList().get(0).getSellerId();
			long userId = placeOrderResp.getUserId();
			// 支付订单
			TradingCallService.payOrder(userId,
					placeOrderResp.getPayableAmount(),
					placeOrderResp.getTradingId());
			// 接单
			TradingCallService.sellerAcceptOrder(sellerId, orderId);
			TradingSyncService.getSyncResult(orderId);
			// 拼邮国际段发货
			TradingCallService.delivery(sellerId, orderId,
					LogisticsTypeEnum.OVERSEAS_DELIVERY, 1);
			TradingSyncService.getSyncResult(orderId);
			// 拼邮国内段发货
			TradingCallService.delivery(sellerId, orderId,
					LogisticsTypeEnum.OVERSEAS_DELIVERY, 3);
			TradingSyncService.getSyncResult(orderId);

			// 确认收货
			TradingCallService.confirmReceive(userId, orderId);
			
			Logger.comment("前置数据准备结束");
			receivertncommissionresultBean.setOrderId((int)orderId);
			receivertncommissionresultBean.setRtnCommissionResult(1);
			receivertncommissionresultCall
					.setData(receivertncommissionresultBean);
			receivertncommissionresultCall.callService();

			Logger.verifyEquals("ok",
					receivertncommissionresultCall.getReturnData(), "验证返回报文");

			YmtSettlementInvoiceWapper defaultWapper = new YmtSettlementInvoiceWapper();

			// 查询交易退款结算表
			List<YmtSettlementInvoice> settlementInvoiceList = defaultWapper
					.selectYmtSettlementInvoice(String.valueOf(orderId));
			Integer rtnCommissionResult = (Integer) settlementInvoiceList
					.stream().filter(m -> (Integer) m.getBusinessType() == 4)
					.collect(Collectors.toList()).get(0).getSettlementStatus();
			Logger.verifyEquals(
					receivertncommissionresultBean.getRtnCommissionResult(),
					rtnCommissionResult, "验证SettlementStatus");
		} catch (Exception e) {
			Logger.fail(e);
		}

	}

	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_ReceiveRtnCommissionResult_002() {
		Logger.start(true, "接收返佣结果类型为已返佣");
		try {

			Logger.comment("前置数据准备");
			// 创建一个拼邮的订单
			TradingServiceResp placeOrderResp = TradingPlaceOrderCallService
					.placeOrderWithLogisticsType(LogisticsTypeEnum.OVERSEAS_DELIVERY);

			long orderId = placeOrderResp.getOrderList().get(0).getOrderId();
			long sellerId = placeOrderResp.getOrderList().get(0).getSellerId();
			long userId = placeOrderResp.getUserId();
			// 支付订单
			TradingCallService.payOrder(userId,
					placeOrderResp.getPayableAmount(),
					placeOrderResp.getTradingId());
			// 接单
			TradingCallService.sellerAcceptOrder(sellerId, orderId);
			TradingSyncService.getSyncResult(orderId);
			// 拼邮国际段发货
			TradingCallService.delivery(sellerId, orderId,
					LogisticsTypeEnum.OVERSEAS_DELIVERY, 1);
			TradingSyncService.getSyncResult(orderId);
			// 拼邮国内段发货
			TradingCallService.delivery(sellerId, orderId,
					LogisticsTypeEnum.OVERSEAS_DELIVERY, 3);
			TradingSyncService.getSyncResult(orderId);

			// 确认收货
			TradingCallService.confirmReceive(userId, orderId);
			
			Logger.comment("前置数据准备结束");
			receivertncommissionresultBean.setOrderId((int)orderId);
			receivertncommissionresultBean.setRtnCommissionResult(2);
			receivertncommissionresultCall
					.setData(receivertncommissionresultBean);
			receivertncommissionresultCall.callService();
			Logger.verifyEquals("ok",
					receivertncommissionresultCall.getReturnData(), "验证返回报文");
			YmtSettlementInvoiceWapper defaultWapper = new YmtSettlementInvoiceWapper();
			// 查询交易退款结算表
			List<YmtSettlementInvoice> settlementInvoiceList = defaultWapper
					.selectYmtSettlementInvoice(String.valueOf(orderId));
			Integer rtnCommissionResult = (Integer) settlementInvoiceList
					.stream().filter(m -> (Integer) m.getBusinessType() == 4)
					.collect(Collectors.toList()).get(0)
					.getSettlementStatus();
			Logger.verifyEquals(
					receivertncommissionresultBean.getRtnCommissionResult(),
					rtnCommissionResult, "验证SettlementStatus");
		} catch (Exception e) {
			Logger.fail(e);
		}

	}

	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_ReceiveRtnCommissionResult_003() {
		Logger.start(true, "接收返佣结果类型为其它状态");
		try {

			// 创建一个拼邮的订单
			TradingServiceResp placeOrderResp = TradingPlaceOrderCallService
					.placeOrderWithLogisticsType(LogisticsTypeEnum.OVERSEAS_DELIVERY);

			long orderId = placeOrderResp.getOrderList().get(0).getOrderId();
			long sellerId = placeOrderResp.getOrderList().get(0).getSellerId();
			long userId = placeOrderResp.getUserId();
			// 支付订单
			TradingCallService.payOrder(userId,
					placeOrderResp.getPayableAmount(),
					placeOrderResp.getTradingId());
			// 接单
			TradingCallService.sellerAcceptOrder(sellerId, orderId);
			TradingSyncService.getSyncResult(orderId);
			// 拼邮国际段发货
			TradingCallService.delivery(sellerId, orderId,
					LogisticsTypeEnum.OVERSEAS_DELIVERY, 1);
			TradingSyncService.getSyncResult(orderId);
			// 拼邮国内段发货
			TradingCallService.delivery(sellerId, orderId,
					LogisticsTypeEnum.OVERSEAS_DELIVERY, 3);
			TradingSyncService.getSyncResult(orderId);

			// 确认收货
			TradingCallService.confirmReceive(userId, orderId);

			Logger.comment("前置数据准备结束");
			receivertncommissionresultBean.setOrderId((int)orderId);
			receivertncommissionresultBean.setRtnCommissionResult(3);
			receivertncommissionresultCall
					.setData(receivertncommissionresultBean);
			receivertncommissionresultCall.callService();
			Logger.verifyEquals("fail",
					receivertncommissionresultCall.getReturnData(), "验证返回报文");
			YmtSettlementInvoiceWapper defaultWapper = new YmtSettlementInvoiceWapper();

			// 查询交易退款结算表
			List<YmtSettlementInvoice> settlementInvoiceList = defaultWapper
					.selectYmtSettlementInvoice(String.valueOf(orderId));
			Integer rtnCommissionResult = (Integer) settlementInvoiceList
					.stream().filter(m -> (Integer) m.getBusinessType() == 4)
					.collect(Collectors.toList()).get(0)
					.getSettlementStatus();
			Logger.verifyEquals(null, rtnCommissionResult, "验证SettlementStatus");

		} catch (Exception e) {
			Logger.fail(e);
		}

	}

	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_ReceiveRtnCommissionResult_004() {
		Logger.start(true, "接收返佣结果类型为null");
		try {

			Logger.comment("前置数据准备");
			// 创建一个拼邮的订单
			TradingServiceResp placeOrderResp = TradingPlaceOrderCallService
					.placeOrderWithLogisticsType(LogisticsTypeEnum.OVERSEAS_DELIVERY);

			long orderId = placeOrderResp.getOrderList().get(0).getOrderId();
			long sellerId = placeOrderResp.getOrderList().get(0).getSellerId();
			long userId = placeOrderResp.getUserId();
			// 支付订单
			TradingCallService.payOrder(userId,
					placeOrderResp.getPayableAmount(),
					placeOrderResp.getTradingId());
			// 接单
			TradingCallService.sellerAcceptOrder(sellerId, orderId);
			TradingSyncService.getSyncResult(orderId);
			// 拼邮国际段发货
			TradingCallService.delivery(sellerId, orderId,
					LogisticsTypeEnum.OVERSEAS_DELIVERY, 1);
			TradingSyncService.getSyncResult(orderId);
			// 拼邮国内段发货
			TradingCallService.delivery(sellerId, orderId,
					LogisticsTypeEnum.OVERSEAS_DELIVERY, 3);
			TradingSyncService.getSyncResult(orderId);

			// 确认收货
			TradingCallService.confirmReceive(userId, orderId);

			Logger.comment("前置数据准备结束");
			receivertncommissionresultBean.setOrderId((int)orderId);
			// receivertncommissionresultBean.setRtnCommissionResult(3);
			receivertncommissionresultCall
					.setData(receivertncommissionresultBean);
			receivertncommissionresultCall.callService();
			Logger.verifyEquals("fail",
					receivertncommissionresultCall.getReturnData(), "验证返回报文");
			YmtSettlementInvoiceWapper defaultWapper = new YmtSettlementInvoiceWapper();
			// 查询交易退款结算表
			List<YmtSettlementInvoice> settlementInvoiceList = defaultWapper
					.selectYmtSettlementInvoice(String.valueOf(orderId));
			Integer rtnCommissionResult = (Integer) settlementInvoiceList
					.stream().filter(m -> (Integer) m.getBusinessType() == 4)
					.collect(Collectors.toList()).get(0)
					.getSettlementStatus();
			Logger.verifyEquals(null, rtnCommissionResult, "验证SettlementStatus");
		} catch (Exception e) {
			Logger.fail(e);
		}

	}
}
